package com.dayuanit.pay.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayuanit.pay.domain.PayOrder;
import com.dayuanit.pay.dto.AjaxResultDTO;
import com.dayuanit.pay.exception.MallBusException;
import com.dayuanit.pay.service.BankPayService;
import com.dayuanit.pay.service.PayService;

@Controller
@RequestMapping("/payOrder")
public class PayOrderController {
	
	private static final Logger log = LoggerFactory.getLogger(PayOrderController.class);
	
	@Autowired
	private PayService payService;
	
	@Autowired
	private BankPayService bankPayService;
	
	@Value("#{configProperties['dayuanit.pay.topay.url']}")
	private String payUrl;
	
	@Value("#{configProperties['sinapay_synch_url']}")
	private String sychUrl;
	
	@RequestMapping("/toPay")
	public String toPay(int payId, HttpServletRequest request) {
		PayOrder payOrder = payService.getPayOrder(payId);
		if (null == payOrder) {
			request.setAttribute("errorMsg", "订单不存在!");
			return "error";
		}
		
		request.setAttribute("payId", payId);
		request.setAttribute("amount", payOrder.getAmount());
		return "pay";
	}
	
//	@RequestMapping("/createPayOrder")
//	@ResponseBody
//	public AjaxResultDTO createPayOrder(PayOrder payOrder) {
//		payService.addPayOrder(payOrder);
//		
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("payOrderUrl", payUrl + "?payId=" + payOrder.getId());
//		map.put("payId", payOrder.getId());
//		
//		return AjaxResultDTO.success(map);
//	}
	
	@RequestMapping("/confirmPay")
	@ResponseBody
	public AjaxResultDTO confirmPay(int payId, String payPasswrod) {
		
		try {
			String bankForm = bankPayService.pay(payId, payPasswrod);
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("bankForm", bankForm);
			
			return AjaxResultDTO.success(map);
		} catch(MallBusException me) {
			return AjaxResultDTO.failed(me.getMessage());
		} catch (Exception e) {
			log.error("支付异常,{}", e.getMessage(), e);
			return AjaxResultDTO.failed("支付失败");
		}
		
	}
	
	@RequestMapping("/sinaNotify")
	@ResponseBody
	public Map sinaNotify(String data) {
		
		log.info("新浪异步通知信息：{}", data);
		
		Map<String, Object> map = new HashMap<>();
		map.put("result", "2");
		map.put("redirecturl", sychUrl);
		
		//TODO 修改支付订单的状态
		try {
			bankPayService.processSinaNotify(data);
			map.put("result", "1");
		} catch(MallBusException me) {
			log.error(me.getMessage(), me);
		} catch(Exception e) {
			log.error("处理新浪支付异步通知异常{}", e.getMessage(), e);
		}
		
		return map;
	}
	
	public static void main(String[] args) {
		System.out.println(System.currentTimeMillis());
	}

}
